//
//  AIFCreateOrderManager.m
//  PywSdk
//
//  Created by liangyidong on 16/4/21.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "AIFCreateOrderManager.h"

@implementation AIFCreateOrderManager

NSString *const CreateOrder_SIGN = @"sign";
NSString *const CreateOrder_CP_Price = @"cp_price";
NSString *const CreateOrder_Charge_Type = @"charge_type";
NSString *const CreateOrder_Passport = @"passport";
NSString *const CreateOrder_Account = @"account";
NSString *const CreateOrder_Credit = @"credit";
NSString *const CreateOrder_Couponids = @"couponids";
NSString *const CreateOrder_CP_Params = @"cp_params";
NSString *const CreateOrder_Pay_Type = @"pay_type";
NSString *const CreateOrder_CP_Prodid = @"cp_prodid";
NSString *const CreateOrder_Order_Desc = @"order_desc";
NSString *const CreateOrder_TID = @"tid";
NSString *const CreateOrder_Passport_Token = @"passport_token";
NSString *const CreateOrder_Token = @"token";
NSString *const CreateOrder_GameId = @"gameid";
NSString *const CreateOrder_RoleName = @"rolename";
NSString *const CreateOrder_Service = @"service";
NSString *const CreateOrder_RoleID = @"roleid";
NSString *const CreateOrder_ServiceID = @"serverArea";
NSString *const CreateOrder_OrderID = @"cp_order_id";

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.child = self;
        self.paramSource = self;
        self.validate = self;
    }
    return self;
}


#pragma mark - ETAPIManager
- (NSString *)methodName{
    return URL_CREATEORDER;
}

- (ETAPIManagerRequestType)requestType{
    return ETAPIManagerRequestTypePost;
}


- (NSString *)seriviceType{
    return [PywAppInfo sharedAppInfo].seriviceType;
}

#pragma mark - ETAPIManagerParamSourceDelegate
- (NSDictionary *)paramsForApi:(ETApiBaseManager *)manager{
    
    PLog(@"couponNumbers:%@",self.payInfo.couponNumbers);
    
    NSString * tid = [PywAppInfo sharedAppInfo].tid;
    
    // 避免多次循环if语句
    NSMutableString * couponids = [NSMutableString stringWithString:@""];
    for (int i = 0; i < self.payInfo.couponNumbers.count; i++) {
        if (couponids.length == 0) {
            [couponids appendString:[NSString stringWithFormat:@"%@",self.payInfo.couponNumbers[i]]];
        }else {
            [couponids appendString:[NSString stringWithFormat:@",%@",self.payInfo.couponNumbers[i]]];
        }
    }
    
    NSString *gameid = [PywAppInfo sharedAppInfo].gameId;
    NSString *account = [PywPlatformMain sharedPlatformMain].account;
    NSString *charge_type = self.payInfo.cpprodid == nil ? [@(2) stringValue]:[@(1) stringValue];
    NSString *apiSeret2 = [AppUtil getApiSecret2:tid apiSecret:[PywAppInfo sharedAppInfo].apisecret];
    NSString *cpprodid = self.payInfo.cpprodid?self.payInfo.cpprodid:@"0";//这个就可以推出任意充还是固定充
    NSString *cporderid = self.payInfo.order;
    
    NSString *sign = [PywCipherUtils md5:[NSString stringWithFormat:@"%@%@%@%@%@", account, charge_type, couponids, cpprodid, apiSeret2]];

    NSString *cp_price = [NSString stringWithFormat:@"%.f",self.payInfo.amount];
    
    NSString *passport = [PywPlatformMain sharedPlatformMain].passport;
    NSString *credit = [NSString stringWithFormat:@"%d",self.credit];
    NSInteger pay_type = self.payInfo.payMethod;
    NSString *order_desc = self.payInfo.productDescript;
//    NSData * para_data = [NSJSONSerialization dataWithJSONObject:self.payInfo.cpprams options:0 error:nil];
//    NSString *para_data_str = [[NSString alloc] initWithData:para_data encoding:NSUTF8StringEncoding];
//    para_data_str = [para_data_str URLEncode];
    NSString *para_data_str = [self.payInfo.cpprams URLEncode];
    
    NSString *rolename = [self.payInfo.rolename URLEncode];
    NSString *service = [self.payInfo.service URLEncode];
    NSString *roleId = [self.payInfo.roleid URLEncode];
    NSString *serviceId = [self.payInfo.serverArea URLEncode];
    
    MutableDictionary *param = [[MutableDictionary alloc] init];
    [param setObject:sign forKey:CreateOrder_SIGN];
    [param setObject:cp_price forKey:CreateOrder_CP_Price];
    [param setObject:charge_type forKey:CreateOrder_Charge_Type];
    [param setObject:passport forKey:CreateOrder_Passport];
    [param setObject:account forKey:CreateOrder_Account];
    [param setObject:credit forKey:CreateOrder_Credit];
    [param setObject:[NSString stringWithFormat:@"%@",couponids] forKey:CreateOrder_Couponids];
    [param setObject:para_data_str forKey:CreateOrder_CP_Params];
    [param setObject:[@(pay_type) stringValue] forKey:CreateOrder_Pay_Type];
    [param setObject:cpprodid forKey:CreateOrder_CP_Prodid];
    [param setObject:order_desc forKey:CreateOrder_Order_Desc];
    [param setObject:gameid forKey:CreateOrder_GameId];
    [param setObject:tid forKey:CreateOrder_TID];
    [param setObject:cporderid forKey:CreateOrder_OrderID];
    if ([PywPlatformMain sharedPlatformMain].accountType == UserType_Passport) {
        [param setObject:[PywPlatformMain sharedPlatformMain].passport_Token forKey:CreateOrder_Passport_Token];
    }else if([PywPlatformMain sharedPlatformMain].accountType == UserType_Account){
        [param setObject:[PywPlatformMain sharedPlatformMain].token forKey:CreateOrder_Token];
        [param setObject:@"" forKey:CreateOrder_Passport];
    }
    [param setObject:rolename forKey:CreateOrder_RoleName];
    [param setObject:service forKey:CreateOrder_Service];
    [param setObject:roleId forKey:CreateOrder_RoleID];
    [param setObject:serviceId forKey:CreateOrder_ServiceID];
    PLog(@"Create_Param:%@",param);
    return param;
}

#pragma mark - ETAPIManagerValidator
- (BOOL)manager:(ETApiBaseManager *)manager isCorrectWithParamsData:(NSDictionary *)data{
    return YES;
}

- (BOOL)manager:(ETApiBaseManager *)manager isCorrectWithCallBackData:(NSDictionary *)data{
    return YES;
}

@end
